dnl Macro: AC_CHECK_IODBC
dnl Checks for iodbc optionally in provided directories
dnl sets shell variable iodbc_ok to yes or no.
dnl Defines HAVE_LIBIODBC, HAVE_ISQL_H, HAVE_ISQLEXT_H if they are found
AC_DEFUN(AC_CHECK_IODBC,
[
AC_LANG_SAVE
AC_LANG_C
AC_ARG_WITH(iodbc,
[  --with-iodbc[=DIR]      Use iodbc, optionally installed in DIR],
[
if test "x$withval" != "xyes"
then
	iodbc_dir=$withval
else
	iodbc_dir="/usr/local"
fi
])

if test "x$iodbc_dir" = "x"
then
	iodbc_dir="/usr/local"
fi

AC_ARG_WITH(iodbc-includes,
[  --with-iodbc-includes=DIR Find iodbc headers in DIR],
[iodbc_includes_dir=$withval],
[iodbc_includes_dir="$iodbc_dir/include"]
)

AC_ARG_WITH(iodbc-libraries,
[  --with-iodbc-libraries=DIR Find iodbc libraries in DIR],
[iodbc_libraries_dir=$withval],
[iodbc_libraries_dir="$iodbc_dir/lib"]
)

save_CPPFLAGS="$CPPFLAGS"
save_LIBS="$LIBS"

CPPFLAGS="$CPPFLAGS -I$iodbc_includes_dir"
LIBS="$LIBS -L$iodbc_libraries_dir"

AC_CHECK_HEADERS([isql.h isqlext.h],
[iodbc_ok=yes; odbc_headers="$odbc_headers $ac_hdr"],
[iodbc_ok=no; break])

if test "x$iodbc_ok" = "xyes"
then
	AC_CHECK_LIB(iodbc,SQLConnect,[iodbc_ok=yes],[iodbc_ok=no])
fi

if test "x$iodbc_ok" = "xyes"
then
	LIBS="$LIBS -liodbc"
	AC_DEFINE(HAVE_LIBIODBC,1,Defined if using iodbc)
	AC_DEFINE(HAVE_ISQL_H,1,Defined if using iodbc)
	AC_DEFINE(HAVE_ISQLEXT_H,1,Defined if using iodbc)
else
	CPPFLAGS="$save_CPPFLAGS"
	LIBS="$save_LIBS"
fi
AC_LANG_RESTORE
])

dnl Macro: AC_CHECK_ODBC_TYPE
dnl Checks if $1 is a valid type in the ODBC environment,
dnl and #defines it to $2 if not

AC_DEFUN(AC_CHECK_ODBC_TYPE,
[

AC_MSG_CHECKING([for $1])
AC_CACHE_VAL(ac_cv_odbc_$1,
[
AC_LANG_SAVE
AC_LANG_C
echo > conftest.c

for i in $odbc_headers
do
	echo "#include <$i>" >> conftest.c
done

echo "int main(void) { $1 x; return 0; }" >> conftest.c

if $CC -c $CFLAGS $CPPFLAGS conftest.c > /dev/null 2> /dev/null
then
	eval ac_cv_odbc_$1=yes
else
	eval ac_cv_odbc_$2=no
fi
rm -f conftest*
AC_LANG_RESTORE

])

eval ac_odbc_check_res=$ac_cv_odbc_$1
if test "x$ac_odbc_check_res" = "xyes"
then
	AC_MSG_RESULT(yes)
else
	AC_MSG_RESULT([no ($2)])
	AC_DEFINE($1,$2, No description)
fi
])
AC_DEFUN(AC_CHECK_ODBC,
[
AC_LANG_SAVE
AC_LANG_C
AC_ARG_WITH(odbc,
[  --with-odbc[=DIR]       Use unixODBC, optionally installed in DIR],
[
if test "x$withval" != "xyes"
then
	odbc_dir=$withval
else
	odbc_dir="/usr/local"
fi
odbc_libraries_dir="$odbc_dir/lib"
odbc_includes_dir="$odbc_dir/include"
], [ odbc_dir=yes ] )

AC_ARG_WITH(odbc-includes,
[  --with-odbc-includes=DIR Find unixODBC headers in DIR],
[odbc_includes_dir=$withval]
)

AC_ARG_WITH(odbc-libraries,
[  --with-odbc-libraries=DIR Find unixODBC libraries in DIR],
[odbc_libraries_dir=$withval]
)

if test "x$odbc_dir" != "xno"; then
  save_CPPFLAGS="$CPPFLAGS"
  save_LIBS="$LIBS"

  if test "x$odbc_includes_dir" != "x" -a "x$odbc_includes_dir" != "x/usr/include"
  then
    CPPFLAGS="$CPPFLAGS -I$odbc_includes_dir"
  fi

  if test "x$odbc_libraries_dir" != "x"
  then
    LIBS="$LIBS -L$odbc_libraries_dir"
  fi

  AC_CHECK_HEADERS([sql.h sqlext.h sqlucode.h],
  [odbc_ok=yes; odbc_headers="$odbc_headers $ac_hdr"],
  [odbc_ok=no; break]
  )

  if test "x$odbc_ok" = "xyes"
  then
    AC_CHECK_LIB(odbc,SQLConnect,[odbc_ok=yes],[odbc_ok=no])
  fi
else
  odbc_ok=no
fi

AC_MSG_CHECKING([whether unixODBC should be used])
if test "x$odbc_ok" = "xyes"
then
	LIBS="$LIBS -lodbc"
	AC_DEFINE(HAVE_LIBODBC,,defined if using unixodbc)
	AC_DEFINE(HAVE_SQL_H,,)
	AC_DEFINE(HAVE_SQLEXT_H,,)
	AC_MSG_RESULT(yes)
else
	CPPFLAGS="$save_CPPFLAGS"
	LIBS="$save_LIBS"
	AC_MSG_RESULT(no)
fi
AC_LANG_RESTORE
])
